<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="padding: 10px 0">
              <el-button
                v-if="canClick('p-ro-add')"
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRoleAct"
              >
                新增角色
              </el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template #default="{ row }">
                  <el-button
                    size="small"
                    type="success"
                    @click="attributePermission(row)"
                  >
                    分配权限
                  </el-button>
                  <el-button size="small" type="primary" @click="editRole(row)">
                    编辑
                  </el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="delRole(row.id)"
                  >
                    删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                :total="total"
                :current-page="query.page"
                :page-size="query.pagesize"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司管理">
            <h1>公司管理</h1>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 新增角色弹出层 -->
    <el-dialog
      title="新增角色"
      :visible="showpop"
      width="60%"
      @close="closepop"
    >
      <el-form
        ref="roleForm"
        :model="roleForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="showpop = false">取消</el-button>
        <el-button type="primary" @click="submitadd">添加</el-button>
      </span>
    </el-dialog>
    <attribute-permission
      ref="attrper"
      :showattribute.sync="showattributePermission"
    />
  </div>
</template>
<script>

import { getRoleList, deleteRole, addRole, getRoleDetail, updateRole } from '@/api/setting'
import AttributePermission from './components/attribute-permission.vue'
export default {
  components: {
    AttributePermission
  },
  data () {
    return {
      showattributePermission: false,
      query: { page: 1, pagesize: 10 },
      list: [],
      total: 0,
      showpop: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.getRole()
  },
  methods: {
    async attributePermission (row) {
      this.showattributePermission = true
      this.$refs.attrper.setChecked(row.id)
    },
    async editRole (row) {
      try {
        const res = await getRoleDetail(row.id)
        this.roleForm = res
        this.addRoleAct()
      } catch (error) {
        console.log(error)
      }
    },
    addRoleAct () {
      this.showpop = true
    },
    async submitadd () {
      try {
        if (this.roleForm.id) {
          await updateRole(this.roleForm)
        } else {
          await addRole(this.roleForm)
        }
        this.$message.success('添加成功')
        this.showpop = false
        this.getRole()
      } catch (error) {
        console.log(error)
      }
    },
    closepop () {
      this.showpop = false
      try {
        this.$refs.roleForm.resetFields()
        this.roleForm = {
          name: '',
          description: ''
        }
        console.log('重置表单')
      } catch (error) {
        console.log(error)
      }
    },
    async delRole (id) {
      try {
        await this.$confirm('确认删除吗')
        console.log('确认删除')
        await deleteRole(id)
        this.$message.success('删除成功')
        this.getRole()
      } catch (error) {
        console.log(error)
      }
    },
    changePage (currpage) {
      this.query.page = currpage
      this.getRole()
    },
    async getRole () {
      try {
        const res = await getRoleList(this.query)
        this.list = res.rows
        this.total = res.total
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
